import { getRegisterCode } from '@/api/code'
import { ref } from 'vue'

export default () => {
  const getRegisterCodeListLoading = ref(false)
  const getRegisteredCodeListParam = ref<CodeAPIRequest.GetRegisterCodeListByCustomerId>({
    limit: 50,
    page: 1,
    code: '',
    model: '',
  })
  const registeredCodeList = ref<Array<CodeRegisteredCodeList>>([])
  const registeredCodeListTotal = ref(0)

  const getRegisterCodeList = async (userId: string) => {
    getRegisterCodeListLoading.value = true
    const res = await getRegisterCode(getRegisteredCodeListParam.value, userId)
    getRegisterCodeListLoading.value = false
    registeredCodeList.value = res.data.data.rows
    registeredCodeListTotal.value = Number(res.data.data.total)
  }

  /**
   * 重置筛选条件
   */
  const resetParam = () => {
    getRegisteredCodeListParam.value = {
      limit: 50,
      page: 1,
      code: '',
      model: '',
    }
  }

  return {
    registeredCodeList,
    registeredCodeListTotal,
    getRegisterCodeListLoading,
    getRegisteredCodeListParam,
    resetParam,
    getRegisterCodeList,
  }
}
